﻿#include "CLS_WgtTreeWidget.h"
#include "ui_CLS_WgtTreeWidget.h"
#include <QHeaderView>
#include <QStyleFactory>
#include <QCheckBox>
#include <QHBoxLayout>
#include "CLS_WgtItemToolBar.h"
#include <QEvent>
#include <QMouseEvent>
#include <QDebug>
#include <algorithm>
#include <QStackedWidget>
#include <QSpacerItem>

const QString CONST_qstrSearchBtnIcon = "icon_search.png"; //搜索按钮图标
const QString CONST_qstrClearBtnIcon = "edit_close.png";   //清空输入按钮图标
const int CONST_iSearchItemHeight = 38;                    //一条搜索结果控件高度
const int CONST_iSearchListMaxHeight = 300;                //搜索结果最大高度
const int CONST_iSearchInputMaxLength = 100;               //最多搜索输入字节长度

CLS_WgtTreeWidget::CLS_WgtTreeWidget(QWidget *_pParent, bool _blChecked, bool _blDrag, bool _blSearched, bool _blRightMenu, bool _blDrop, n_ItemBtnShowType _iBtnShowType)
    : QWidget (_pParent)
    , ui(new Ui::CLS_WgtTreeWidget)
    , m_blSearchEnable(_blSearched)
{
    ui->setupUi(this);
    m_pWgtTreeWidget = new CLS_WgtBaseTreeWidget(this, _blChecked, _blDrag, _blDrop, _blRightMenu, _iBtnShowType);
    if(_blRightMenu)
    {
        //
    }

    LoadPicPath();
    InitView();
    ConnectSignals();
    LoadLang();
    LoadStyle();
}

CLS_WgtTreeWidget::~CLS_WgtTreeWidget()
{
    qApp->removeTranslator(&m_qtTranslator);
}

int CLS_WgtTreeWidget::LoadStyle()
{
    return 0;
}

void CLS_WgtTreeWidget::LoadPicPath()
{
    m_qstrPicturePath = QString("%1/resource/skin/dark/WgtTreeWidget/images/").arg(qApp->applicationDirPath());
    QString qstrskin = qApp->property("skin").toString();
    if(!qstrskin.isEmpty())
    {
        m_qstrPicturePath = QString("%1/resource/skin/%2/WgtTreeWidget/images/").arg(qApp->applicationDirPath()).arg(qstrskin);
        if(!QFile::exists(m_qstrPicturePath))
        {
            m_qstrPicturePath = QString("%1/resource/skin/dark/WgtTreeWidget/images/").arg(qApp->applicationDirPath());
        }
    }
}

void CLS_WgtTreeWidget::InitView()
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    ui->qvboxlayoutTreeLayout->addWidget(m_pWgtTreeWidget);
    ui->qlineedtQueryInput->setContextMenuPolicy(Qt::NoContextMenu);
    ui->qlineedtQueryInput->setMaxLength(CONST_iSearchInputMaxLength);  //最多只能输入100个字符
    ui->qlistWgtQueryResult->setMaximumHeight(CONST_iSearchListMaxHeight);
    ui->qlistWgtQueryResult->hide();

    QHBoxLayout *topHLayout = new QHBoxLayout(ui->qlineedtQueryInput);
    topHLayout->setContentsMargins(0, 0, 0, 0);
    topHLayout->setSpacing(0);

    int iHeight = ui->qlineedtQueryInput->height();
    QSize qsizeMax(iHeight,iHeight);
    QIcon qiconSearch;
    qiconSearch.addFile(m_qstrPicturePath + CONST_qstrSearchBtnIcon);
    m_pSearchBtn = new QToolButton(ui->qlineedtQueryInput);
    m_pSearchBtn->setIcon(qiconSearch);
    m_pSearchBtn->setCursor(QCursor(Qt::PointingHandCursor));
    m_pSearchBtn->setMaximumSize(qsizeMax);
    m_pSearchBtn->setObjectName("qtoolbtnSearch");

    QIcon qiconClose;
    qiconClose.addFile(m_qstrPicturePath + CONST_qstrClearBtnIcon);
    m_pClearBtn = new QToolButton(ui->qlineedtQueryInput);
    m_pClearBtn->setIcon(qiconClose);
    m_pClearBtn->setCursor(QCursor(Qt::PointingHandCursor));
    m_pClearBtn->setMaximumSize(qsizeMax);
    m_pClearBtn->setObjectName("qtoolbtnClear");

    topHLayout->addWidget(m_pClearBtn,1,Qt::AlignRight);
    topHLayout->addWidget(m_pSearchBtn,0,Qt::AlignRight);

    ClearSearch();
    SetSearchVisible(m_blSearchEnable);
}

void CLS_WgtTreeWidget::ConnectSignals()
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    //搜索框文本改变
    connect(ui->qlineedtQueryInput, &QLineEdit::returnPressed, this, &CLS_WgtTreeWidget::ExecuteSearch);
    //点击搜索框的清除按钮
    connect(m_pClearBtn,&QToolButton::clicked,this,&CLS_WgtTreeWidget::ClearSearch);
    //点击搜索框的搜索按钮
    connect(m_pSearchBtn,&QToolButton::clicked,this,&CLS_WgtTreeWidget::ExecuteSearch);
    //节点单击
    connect(m_pWgtTreeWidget, &CLS_WgtBaseTreeWidget::SignClicked, this, &CLS_WgtTreeWidget::SignClicked);
    //节点双击
    connect(m_pWgtTreeWidget, &CLS_WgtBaseTreeWidget::SignDblClicked, this, &CLS_WgtTreeWidget::SignDblClicked);
    //节点勾选状态改变
    connect(m_pWgtTreeWidget, &CLS_WgtBaseTreeWidget::SignCheckedStatusChanged, this, &CLS_WgtTreeWidget::SignCheckedStatusChanged);
    //节点选中
    connect(m_pWgtTreeWidget, &CLS_WgtBaseTreeWidget::SignSelected, this, &CLS_WgtTreeWidget::SignSelected);
    //右键菜单
    connect(m_pWgtTreeWidget, &CLS_WgtBaseTreeWidget::SignRightClicked, this, &CLS_WgtTreeWidget::SignRightClicked);
    //ToolBar中的按钮被点击
    connect(m_pWgtTreeWidget, &CLS_WgtBaseTreeWidget::SignClickButton, this, &CLS_WgtTreeWidget::SignClickButton);
    //拖放
    connect(m_pWgtTreeWidget, &CLS_WgtBaseTreeWidget::SignDroped, this, &CLS_WgtTreeWidget::SignDroped);
}

void CLS_WgtTreeWidget::LoadLang()
{
    QString qstrCurrLang = qApp->property("language").toString();
    QString qstrQMPath = qApp->applicationDirPath()+ "/Lang/WgtTreeWidget/WgtTreeWidget_" + qstrCurrLang+".qm";

    if (QFile::exists(qstrQMPath))
    {
        if (m_qtTranslator.load(qstrQMPath))
        {
            qApp->installTranslator(&m_qtTranslator);
        }
    }
}

int CLS_WgtTreeWidget::Init(const QString &_qstrRootId, const QMap<QString, CLS_TreeItem> &_qmapTreeItems, bool _blRootNodeVisible)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->Init(_qstrRootId, _qmapTreeItems, _blRootNodeVisible);
}

int CLS_WgtTreeWidget::GetItemById(const QString &_qstrId, CLS_TreeItem &_clsItem)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->GetItemById(_qstrId, _clsItem);
}

int CLS_WgtTreeWidget::GetItemByName(const QString &_qstrName, QVector<CLS_TreeItem> &_qvecItems)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->GetItemByName(_qstrName, _qvecItems);
}

int CLS_WgtTreeWidget::GetCurrentItem(CLS_TreeItem &_clsItem)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->GetCurrentItem(_clsItem);
}

int CLS_WgtTreeWidget::GetAllItems(QMap<QString, CLS_TreeItem> &_qmapTreeItems)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->GetAllItems(_qmapTreeItems);
}

int CLS_WgtTreeWidget::GetCheckedItems(QVector<CLS_TreeItem> &_qvecItems, bool _blOnlyLeafNode)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->GetCheckedItems(_qvecItems, _blOnlyLeafNode);
}

int CLS_WgtTreeWidget::GetCheckedNodes(QVector<CLS_TreeItem> &_qvecItems)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    //只要勾选的叶子节点
    return GetCheckedItems(_qvecItems, true);
}

void CLS_WgtTreeWidget::Clear()
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->Clear();
}

void CLS_WgtTreeWidget::SetItemExpandStatus(const QString &_qstrId, n_TreeExpandType _iExpandType)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->SetItemExpandStatus(_qstrId, _iExpandType);
}

void CLS_WgtTreeWidget::SetAllExpandStatus(bool _blShow)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->SetAllExpandStatus(_blShow);
}

void CLS_WgtTreeWidget::SetShowItemByName(const QString &_qstrName)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->SetShowItemByName(_qstrName);
}

void CLS_WgtTreeWidget::AddItem(const CLS_TreeItem &_clsTreeItem)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->AddItem(_clsTreeItem);
}

void CLS_WgtTreeWidget::AddItems(const QVector<CLS_TreeItem> &_qvecTreeItems)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->AddItems(_qvecTreeItems);
}

void CLS_WgtTreeWidget::DeleteItem(const QString &_qstrId)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->DeleteItem(_qstrId);
}

void CLS_WgtTreeWidget::UpdateItem(const CLS_TreeItem &_clsItem)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->UpdateItem(_clsItem);
}

void CLS_WgtTreeWidget::UpdateItemIcon(const QString &_qstrId, const QString &_qstrIcon)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    m_pWgtTreeWidget->UpdateItemIcon(_qstrId, _qstrIcon);
}

void CLS_WgtTreeWidget::SetCurrentItem(const QString _qstrId, bool _blSend)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->SetCurrentItem(_qstrId, _blSend);
}

void CLS_WgtTreeWidget::SetItemCheckStatus(const QString &_qstrId, Qt::CheckState _iState, bool _blSend)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->SetItemCheckStatus(_qstrId, _iState, _blSend);
}

void CLS_WgtTreeWidget::SetShowItemById(const QString &_qstrId)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->SetShowItemById(_qstrId);
}

void CLS_WgtTreeWidget::SetSearchVisible(bool _blVisible)
{
    //隐藏搜索控件
    if (!_blVisible)
    {
        ui->qlineedtQueryInput->hide();
        return;
    }
    ui->qlineedtQueryInput->show();
    ui->qlineedtQueryInput->setFocus();
}

void CLS_WgtTreeWidget::ClearSearch()
{
    ui->qlineedtQueryInput->clear();//清空编辑框
    ui->qlistWgtQueryResult->clear();
    ui->qlistWgtQueryResult->hide();
    m_pClearBtn->hide();
}

void CLS_WgtTreeWidget::ExecuteSearch()
{
    QString qstrQueryText = ui->qlineedtQueryInput->text();
    if (qstrQueryText.isEmpty())
    {
        return;
    }

    //模糊搜索
    ui->qlistWgtQueryResult->clear();
    QVector<CLS_TreeItem> qvecSearchResult;
    int iRet = GetItemByName(qstrQueryText, qvecSearchResult);
    if (iRet < 0 || qvecSearchResult.size() <= 0)
    {
        //没结果
        QListWidgetItem *pItem = new QListWidgetItem (ui->qlistWgtQueryResult);
        pItem->setText(tr("无结果"));
        ui->qlistWgtQueryResult->setFixedHeight(CONST_iSearchItemHeight +3);
        ui->qlistWgtQueryResult->addItem(pItem);
        ui->qlistWgtQueryResult->show();
        return;
    }

    int iHeight = CONST_iSearchItemHeight*qvecSearchResult.size()+3;//+3为了防止出现滚动
    if (iHeight >= CONST_iSearchListMaxHeight)
    {
        iHeight = CONST_iSearchListMaxHeight;
    }

    ui->qlistWgtQueryResult->setFixedHeight(iHeight);
    //刷新结果列表
    for (int i = 0; i < qvecSearchResult.size(); ++i)
    {
        QListWidgetItem *pItem = new QListWidgetItem (ui->qlistWgtQueryResult);
        pItem->setData(Qt::UserRole, QVariant(qvecSearchResult.at(i).m_qstrId));
        pItem->setText(qvecSearchResult.at(i).m_qstrName);
        ui->qlistWgtQueryResult->addItem(pItem);
    }

    ui->qlistWgtQueryResult->show();
}

void CLS_WgtTreeWidget::on_qlineedtQueryInput_textChanged(const QString &_qstrText)
{
    if (_qstrText.isEmpty())
    {
        ClearSearch();
        return;
    }

    m_pClearBtn->show();
    ExecuteSearch();
}

void CLS_WgtTreeWidget::on_qlineedtQueryInput_returnPressed()
{
    ExecuteSearch();
}

void CLS_WgtTreeWidget::on_qlistWgtQueryResult_itemDoubleClicked(QListWidgetItem *_pItem)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    ui->qlistWgtQueryResult->hide();
    QVariant qvarData = _pItem->data(Qt::UserRole);
    if(qvarData.isValid())
    {
        QString qstrTreeNodeId = qvarData.toString();
        m_pWgtTreeWidget->SetCurrentItem(qstrTreeNodeId,true);
    }

    ui->qlineedtQueryInput->clear();
}

int CLS_WgtTreeWidget::GetChilds(const QString &_qstrId, QVector<CLS_TreeItem> &_qvecChilds, bool _blRecursive)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->GetChilds(_qstrId, _qvecChilds, _blRecursive);
}

int CLS_WgtTreeWidget::GetParent(const QString &_qstrId, CLS_TreeItem &_parentItem)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->GetParent(_qstrId, _parentItem);
}

int CLS_WgtTreeWidget::GetItemCheckStatus(const QString &_qstrId, Qt::CheckState &_iState)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->GetItemCheckStatus(_qstrId, _iState);
}

void CLS_WgtTreeWidget::SetTextMaxWidth(int _iMaxLength)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    return m_pWgtTreeWidget->SetTextMaxWidth(_iMaxLength);
}

void CLS_WgtTreeWidget::ClearSelect()
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return;
    }

    m_pWgtTreeWidget->clearSelection();
}

void CLS_WgtTreeWidget::ClearAndCloseSearch()
{
    ClearSearch();
    SetSearchVisible(false);
}

void CLS_WgtTreeWidget::SlotResizeView(double _dXScale, double _dYScale)
{
    Q_UNUSED(_dXScale)
    Q_UNUSED(_dYScale)
    LoadStyle();
}

int CLS_WgtTreeWidget::MoveUpOneRow(const QString &_qstrSrcId, const QString &_qstrDesId)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->MoveUpOneRow(_qstrSrcId, _qstrDesId);
}

int CLS_WgtTreeWidget::MoveDownOneRow(const QString &_qstrSrcId, const QString &_qstrDesId)
{
    if (nullptr == m_pWgtTreeWidget)
    {
        return -1;
    }

    return m_pWgtTreeWidget->MoveDownOneRow(_qstrSrcId, _qstrDesId);
}
